#include <stdlib.h>

# define Error_Rpt_macro(CONTENT,...)						\
	fprintf( 	stderr , 						\
			CONTENT" -- %s :%s() :%d\n" ,				\
			##__VA_ARGS__ ,						\
			__FILE__ , 						\
			__FUNCTION__ , 						\
			__LINE__ );						\
	fflush( stderr );
/*
inline void Error_Rpt( char * format , char * report ){
	unsigned int format_len;
	unsigned int report_len;
	unsigned int totle_len;

#define APPEND_FORMAT " -- %s %s:%d    \n"

	assert(format);
	assert(report);

	format_len = strlen(format);
	report_len = strlen(report);
	totle_len = format_len + report_len + 1;

	char * format_buf = (char * ) malloc( totle_len );
	if( !format_buf ){
		exit(-1);
	}

	bzero(format_buf , totle_len );
	strcpy( format_buf , format );
	strcat( format_buf , APPEND_FORMAT );

	fprintf( stderr , format_buf , report , __FILE__ ,__FUNCTION__  , __LINE__ );
	fflush( stderr );

	free( format_buf );
	return;
}
*/
inline void * malloc_check(unsigned int size){

#define MALLOC_MAX_TRY 5

	unsigned int malloc_times=0;
	void * ret_address = NULL;

	for( malloc_times=0 ; malloc_times < MALLOC_MAX_TRY ; malloc_times++){
		ret_address = malloc( size );
		if( ret_address ){
			break;
		}

	}

	if(  malloc_times >= MALLOC_MAX_TRY || NULL == ret_address ){
		Error_Rpt_macro("malloc failed already %d times" , MALLOC_MAX_TRY );
		exit(-1);
	}

	memset( ret_address , 0 , size );

	return ret_address;
}

inline void * malloc_check_copy( unsigned int size , const void * const data_p ){

	void * src_tmp=NULL;

	assert( !( (size !=0) && (NULL == data_p) ) );

	src_tmp = malloc_check( size );
	memcpy( src_tmp , data_p , size );

	return src_tmp ;
}

inline void * malloc_check_ncopy( unsigned int malloc_size , void * data_p , unsigned int copy_size ){

	void * src_tmp=NULL;

	assert( !( (copy_size !=0) && (NULL == data_p) ) );
	assert( copy_size <= malloc_size );

	src_tmp = malloc_check( malloc_size );
	memcpy( src_tmp , data_p , copy_size );

	return src_tmp ;
}

